import { routerRedux } from 'dva/router';
import { message } from 'antd';
import { saveSeekHelp,submitSeekHelp,querySeekHelp,delSeekHelpInfo ,getSeekHelpInfoById,updateSeekHelpInfoDealStatus} from '../services/seekHelp';

export default {
  namespace: 'seekHelp',

  state: {
    seekHelpAddFormLoading: false,
    loading: true,
    data: {
      list: [],
      pagination: {},
    },
  },

  effects: {

    *getSeekHelp({payload,callback}, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(querySeekHelp, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response.data)
    },
    *fetchDelSeekHelpInfo({ payload ,callback}, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(delSeekHelpInfo, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response)
    },
    *fetchUpdateSeekHelpInfoDealStatus({ payload ,callback}, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(updateSeekHelpInfoDealStatus, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response)
    },
    *fetchSubmitSeekHelp({ payload }, { call, put }) {
      yield put({
        type: 'seekHelpAddForm',
        payload: true,
      });
      const response = yield call(submitSeekHelp, payload);
      yield put({
        type: 'seekHelpAddForm',
        payload: false,
      });
      message.success('提交成功');
    },
    *seekHelpAddForm({ payload }, { put }) {
      if (payload) {
        yield put(routerRedux.push({
          pathname:'/rdSeekHelpInfo/seekAdd',
          query:{
            seekHelpId:payload.seekHelpId,
            showDetail:payload.showDetail,
          }
        }));
      }else{
        yield put(routerRedux.push('/rdSeekHelpInfo/seekAdd'));
      }
    },
    *saveSeekHelp({ payload }, { call, put }) {
      yield put({
        type: 'seekHelpAddForm',
        payload: true,
      });
      yield call(saveSeekHelp, payload);
      yield put({
        type: 'seekHelpAddForm',
        payload: false,
      });
      message.success('保存成功');
    }
    ,
    *fetchGetSeekHelpInfoById({ payload ,callback}, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(getSeekHelpInfoById, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response.data)
    }
  },

  reducers: {
    seekHelpAddForm(state, { payload }) {
      return {
        ...state,
        seekHelpAddFormLoading: payload,
      };
    },
    query(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },


  },
};
